package org.edu.modules.enroll.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.constant.RoleConstant;
import org.edu.common.system.api.ISysBaseAPI;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.enroll.entity.VeDictQuarter;
import org.edu.modules.enroll.entity.VeZsYybf;
import org.edu.modules.enroll.entity.VeZsYybf;
import org.edu.modules.enroll.service.VeDictQuarterService;
import org.edu.modules.enroll.service.VeZsYybfService;
import org.edu.modules.enroll.service.VeZsYybfService;
import org.edu.modules.enroll.vo.ApplyCheckExcelVo;
import org.edu.modules.enroll.vo.ApplyCheckVo;
import org.edu.modules.enroll.vo.VeZsFenbanExcelVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @Description:  预约拜访
 *
 *
 */
@Api(tags="预约拜访")
@RestController
@RequestMapping("enroll/yybf")
@Slf4j
public class VeZsYybfController {

   @Resource
   private VeZsYybfService veZsYybfService;
   @Resource
   private ISysBaseAPI sysBaseAPI;

    @ApiOperation(value="查询预约拜访列表")
    @GetMapping(value = "/list")
    public Result<?> list(VeZsYybf veZsYybf,
                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                          HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser==null){
            return Result.error("未登录或登录已过期");
        }
        LambdaQueryWrapper<VeZsYybf> queryWrapper = new LambdaQueryWrapper<>();

        //权限判断，是否是招就科的人，是则查询无推荐老师的，否则查询推荐老师是自己的
        List<String> roleCodeList = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        if (roleCodeList!=null&&roleCodeList.size()>0&&roleCodeList.contains(RoleConstant.SYS_ROLE_ZJK)){
            queryWrapper.isNull(VeZsYybf::getTjrUserId);
        }else {
            queryWrapper.eq(VeZsYybf::getTjrUserId,sysUser.getUsername());
        }
        if (veZsYybf.getBfEndTime() != null) {
            queryWrapper.le(VeZsYybf::getBfEndTime, veZsYybf.getBfEndTime());
        }
        if (veZsYybf.getBfStaTime() != null) {
            queryWrapper.ge(VeZsYybf::getBfStaTime, veZsYybf.getBfStaTime());
        }
        if (StringUtils.isNotBlank(veZsYybf.getXm())){
            queryWrapper.like(VeZsYybf::getXm, veZsYybf.getXm());
        }
        if (StringUtils.isNotBlank(veZsYybf.getLxdh())){
            queryWrapper.like(VeZsYybf::getLxdh, veZsYybf.getLxdh());
        }
        Page<VeZsYybf> page = new Page<VeZsYybf>(pageNo, pageSize);
        IPage<VeZsYybf> pageList = veZsYybfService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    /**
     * 添加
     *
     * @param veZsYybf
     * @return
     */
    @AutoLog(value = "预约拜访-添加")
    @ApiOperation(value = "预约拜访-添加", notes = "预约拜访-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeZsYybf veZsYybf) {


        // 获取当前登陆用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser==null){
            return Result.error("未登录或登录已过期");
        }
        // 设置操作人
        veZsYybf.setCreateBy(sysUser.getUsername());
//        veZsYybf.setCreateTime(new Date());

        // 从前端获取设置操作人的id和名称
        veZsYybfService.save(veZsYybf);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veZsYybf
     * @return
     */
    @AutoLog(value = "预约拜访-编辑")
    @ApiOperation(value = "预约拜访-编辑", notes = "预约拜访-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeZsYybf veZsYybf) {
        // 获取当前登陆用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser==null){
            return Result.error("未登录或登录已过期");
        }
        veZsYybf.setUpdateBy(sysUser.getUsername());
        veZsYybfService.updateById(veZsYybf);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "预约拜访-通过id删除")
    @ApiOperation(value = "预约拜访-通过id删除", notes = "预约拜访-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        veZsYybfService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "预约拜访-批量删除")
    @ApiOperation(value = "预约拜访-批量删除", notes = "预约拜访-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veZsYybfService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "预约拜访-通过id查询")
    @ApiOperation(value = "预约拜访-通过id查询", notes = "预约拜访-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeZsYybf veZsYybf = veZsYybfService.getById(id);
        if (veZsYybf == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veZsYybf);
    }

    @ApiOperation(value="批量通过")
    @PostMapping(value = "/batchPass")
    public Result<?> batchPass(@ApiParam("拼接的ID字符串") @RequestParam(value = "ids") String ids){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser==null){
            return Result.error("未登录或登录已过期");
        }
        //切割字符串
        String[] split = ids.split(",");
        List<VeZsYybf> list = new ArrayList<>();
        for(String sp : split){

            Long id = Long.parseLong(sp);
            VeZsYybf veZsYybf = new VeZsYybf();
            veZsYybf.setId(id);
            veZsYybf.setApproverTime(new Date());
            veZsYybf.setApproverUserId(sysUser.getUsername());
            veZsYybf.setStatus(2);
            list.add(veZsYybf);

        }
        veZsYybfService.updateBatchById(list);

        return Result.OK("批量通过成功！");

    }

    @ApiOperation(value="批量不通过")
    @PostMapping(value = "/batchNoPass")
    public Result<?> batchNoPass(@ApiParam("拼接的ID字符串") @RequestParam(value = "ids") String ids,@ApiParam("不通过理由") @RequestParam(value = "reason") String reason){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser==null){
            return Result.error("未登录或登录已过期");
        }
        //切割字符串
        String[] split = ids.split(",");
        List<VeZsYybf> list = new ArrayList<>();
        for(String sp : split){

            Long id = Long.parseLong(sp);
            VeZsYybf veZsYybf = new VeZsYybf();
            veZsYybf.setId(id);
            veZsYybf.setApproverTime(new Date());
            veZsYybf.setApproverUserId(sysUser.getUsername());
            veZsYybf.setApproverReason(reason);
            veZsYybf.setStatus(1);
            list.add(veZsYybf);

        }
        veZsYybfService.updateBatchById(list);

        return Result.OK("批量不通过成功！");

    }

}
